import Monster from './Monster'
import MusicMgr from './MusicManager'
import Role from './Player'

export default class GameMgr {
  public static readonly instance: GameMgr = new GameMgr()

  musicMgr: MusicMgr = new MusicMgr()

  audioClip: any = {} //缓存音频

  prefabsMap: Map<string, cc.Prefab[]> = null //当前场景所需的prefabs

  isIphoneX: boolean = false

  canLayoutTopBgcMove: boolean = false

  roleInstance: Role = null

  layoutTop: cc.Node = null

  roleNode: cc.Node = null //人物角色的Node

  roleAnimation: cc.Animation = null //人物角色的Animation

  MonstersAlive: { node: cc.Node; target: Monster }[] = []

  totalPowerLabel: cc.Label = null

  pannelContent: cc.Node = null

  public static LoadPrefab(url: string) {
    return new Promise((resolve, reject) => {
      // 加载预制卡片模版
      cc.loader.loadResDir(url, (err, res) => {
        if (!err) {
          const prefabsMap = GameMgr.instance.prefabsMap
          if (prefabsMap.has(url)) {
            prefabsMap.get(url).concat(res)
          } else {
            prefabsMap.set(url, res)
          }
          resolve(prefabsMap)
        } else {
          reject()
        }
      })
    })
  }

  public static showToast(node, title, btntext, selfcallback) {
    cc.resources.load('prefab/toast/Toast', cc.Prefab, (err, toast: cc.Prefab) => {
      if (!err) {
        let toastNode: cc.Node = cc.instantiate(toast)
        node.addChild(toastNode)
        toastNode.setPosition(0, 0)
        toastNode.active = true
        toastNode.emit('fade-in', {
          title,
          btntext,
          callback: () => {
            selfcallback(toastNode)
          }
        })
      }
    })
  }
}
